<?php

require_once( '../system/smarty.inc' );
require_once( '../system/tdao/Class_T_PATTERN.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_T_PATTERN,$login['staff_auth']);
if   ($login['staff_auth'] == 'G-6' || $login['staff_auth'] == 'G-7')  {
	header("Location: ./tpattern_shop.php");
	exit;
}

/*
if (empty($_GET['matrix_cd']) || empty($_GET['block_cd']) || empty($_GET['sort'])){
	echo "パラメタエラー";
	exit;
}
*/

//================AJAゾーンエリア==================
$login_auth = $login['staff_auth'];

if ($login_auth == 'G-2' ) {
	$area_cd   = $login['autoid'];
	$forms["sort"] = '2';
	$areauser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else if ($login_auth == 'G-3' ) {
	$zone_cd   = $login['autoid'];
	$forms["sort"] = '3';
	$cellsort = array();
	$zoneuser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else {
	// PLEASE_SELECT_ARR ;
	$zoneuser  = array();
	$zoneuser  = Class_M_OTHER::get_ajaeks_array(NULL, 3);
	$areauser = array();
	
	$first_zone_key = null;
	foreach($zoneuser as $key => $value){
    	$first_zone_key = $key;
    	break;
	}
	//if ($zoneuser) {
	//	$areauser =  Class_M_OTHER::get_ajaeks_array(NULL, 2, $zoneuser[0]);
	//}
	if ( isset( $_GET['zone_cd'] ) &&  ( !empty( $_GET['zone_cd'] ) ) ){
		$first_zone_key = $_GET['zone_cd'];
	}
	$areauser = Class_M_OTHER::get_area_array_byzone(PLEASE_SELECT_ARR,$first_zone_key );
	
	
}

//================AJAゾーンエリア==================

$matrix_cd = $_GET['matrix_cd'] ;
$dosearch = FALSE ;
$dao = new Class_T_PATTERN;
$dao->setTableNameWeek($matrix_cd);

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['INS_SHOP'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['INS_SHOP'] = $forms;
}

$area_zone_flg  = 0;
$isG2_3 = FALSE;
if   ($login['staff_auth'] == 'G-2') {
	$area_zone_flg = 2;
	$isG2_3 = TRUE;
}
if   ($login['staff_auth'] == 'G-3') {
	$area_zone_flg = 3;
	$isG2_3 = TRUE;
}

$wherearr =  array();
if ( isset( $forms['block_cd'] ) &&  ( !empty( $forms['block_cd'] ) ) ){
	$wherearr[] = sprintf(" c.block_cd =  '%s' ",  $forms['block_cd'] );
}

if ( isset( $forms['area_cd'] ) &&  ( !empty( $forms['area_cd'] ) ) ){
	$wherearr[] = sprintf(" c.area_aja =  '%s' ",  $forms['area_cd'] );
}

if ( isset( $forms['zone_cd'] ) &&  ( !empty( $forms['zone_cd'] ) ) ){
	$wherearr[] = sprintf(" c.zone_aja =  '%s' ",  $forms['zone_cd'] );
}

if ( isset( $forms['shop_cd'] ) &&  ( !empty( $forms['shop_cd'] ) ) ){
	$wherearr[] = sprintf(" c.shop_cd =  '%s' ",  $forms['shop_cd'] );
}

if ( isset( $forms['smedia_cd'] ) &&  ( !empty( $forms['smedia_cd'] ) ) ){
	$wherearr[] = sprintf(" b.media_cd =  '%s' ",  $forms['smedia_cd'] );
}

if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1, 'd' );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'],2, 'd' );
}
/*
//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "ins_shop".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->search( $wherearr );
	$colums = array();
	//列名
	$colums["name"] = array('ins_co_name','local_name','ins_shop_cd','ins_shop_name','media_name','delive_name','in_area_name','insert_conums','u_time','m_user_name');
	$colums["type"] = array();
	foreach ($colums as $k => $v) {
		$colums["type"][$v] = 1;
	}
	$colums["type"]["insert_conums"] = 2;
	
	
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了
*/


if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];
	$st = ( $page['current'] -1 ) * PAGE_MAX;
}else{
	$page['current'] = 1;
	$st = 0;
}

$page['item'] = PAGE_MAX;

if ($dosearch) {
	$page = NULL;
	//$page内容は関数内部更新されます。
	$data      = $dao->getsearch( $wherearr,   $area_zone_flg );
	$pdatanums = $dao->getPattenNums($wherearr, $area_zone_flg);
	
	$patterns = $dao->getpatterns( $forms['block_cd']);
	foreach ($data as $k=>$v) {
		foreach ($patterns as $kk=>$vv) {
			if (isset($pdatanums[$v["ins_shop_shop_cd"]][$vv["pattern_cd"]])) {
				$data[$k][$vv["col"]] = $pdatanums[$v["ins_shop_shop_cd"]][$vv["pattern_cd"]];
			}else {
				$data[$k][$vv["col"]] =  "";
			}
		}
	}
}
//$select = array(''=>"選択してください");
//折込会社の場合、折込会社の自社です。
//$ins_co = Class_M_OTHER::get_ins_co_array(PLEASE_SELECT_ARR);
$page["cnt"] = count($data);


//==================================周
if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}
$smarty->assign('year4',  Class_PWD::get_years());

$week = Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );

$smarty->assign('week', $week );
//==================================周
if ( empty( $forms["sort"] ) ) {
	$forms["sort"] = "2";
}
  
if( isset($forms) )     $smarty->assign('forms', $forms);
if( isset($data) )      $smarty->assign('data', $data);
if( isset($patterns) )  $smarty->assign('pattern', $patterns);
if( isset($page) )      $smarty->assign('page', $page);
$smarty->assign('isG2_3', $isG2_3 );


$aoki_area_zones = array();
//$aoki_area_zones[""] = "すべて";

foreach ($aoki_area_zone as $k => $v) {
	$aoki_area_zones[$k] = $v;
}
$smarty->assign('sorts', $aoki_area_zone );

if( isset($zoneuser) )    $smarty->assign('zoneuser', $zoneuser);
if( isset($areauser) )    $smarty->assign('areauser', $areauser);
if( isset( $err_mes ) )   $smarty->assign('err', $err_mes);


/*
$ins_co = Class_M_OTHER::get_ins_co_array( PLEASE_SELECT_ARR );
$smarty->assign('ins_co', $ins_co );

$local= Class_M_OTHER::get_local_array( PLEASE_SELECT_ARR );
$smarty->assign('local', $local );
*/

$block = Class_M_OTHER::get_block_array( PLEASE_SELECT_ARR );
$smarty->assign('block', $block );
//リゾームの最大の周のデータ
$pre_matrix = Class_M_OTHER::get_max_rizome_matrix_cd();
if ( empty($pre_matrix) ) {
	$pre_matrix = $matrix_cd ;
}else {
	if ($pre_matrix > $matrix_cd ){
		$pre_matrix = $matrix_cd;
	}
}
$smarty->assign('pre_matrix', $pre_matrix );


$smarty->assign('menu',   $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
//$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('tpattern_index.html');

?>